package net.lab1024.sa.admin.module.business.sh.service;

import java.time.LocalDateTime;
import java.util.List;

import cn.dev33.satoken.stp.StpUtil;
import net.lab1024.sa.admin.module.business.sh.dao.ShDao;
import net.lab1024.sa.admin.module.business.sh.domain.entity.ShEntity;
import net.lab1024.sa.admin.module.business.sh.domain.form.ShAddForm;
import net.lab1024.sa.admin.module.business.sh.domain.form.ShQueryForm;
import net.lab1024.sa.admin.module.business.sh.domain.form.ShUpdateForm;
import net.lab1024.sa.admin.module.business.sh.domain.vo.ShVO;
import net.lab1024.sa.admin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.sa.admin.module.system.employee.service.EmployeeService;
import net.lab1024.sa.admin.module.system.login.service.LoginService;
import net.lab1024.sa.admin.util.IdGenerator;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 审核 Service
 *
 * @Author syf
 * @Date 2025-06-04 14:41:53
 * @Copyright 畅想科技
 */

@Service
public class ShService {

    @Resource
    private ShDao shDao;
    @Resource
    private LoginService loginService;
    @Resource
    private EmployeeService employeeService;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<ShVO> queryUserPage(ShQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ShVO> list = shDao.queryPage(page, queryForm);
        PageResult<ShVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }
    public PageResult<ShVO> queryOrderPage(ShQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ShVO> list = shDao.queryPage(page, queryForm);
        PageResult<ShVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }
    /**
     * 添加
     */
    public ResponseDTO<String> add(ShAddForm addForm) {
        ShEntity shEntity = SmartBeanUtil.copy(addForm, ShEntity.class);
        Long id = IdGenerator.generateId();
        shEntity.setShId(String.valueOf(id));
        shDao.insert(shEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(ShUpdateForm updateForm) {
        updateForm.setShState("approvaied");
        // 获取当前登录用户
        Long requestEmployeeId = loginService.getEmployeeIdByLoginId((String) StpUtil.getLoginId());
        EmployeeEntity employeeEntity = employeeService.getById(requestEmployeeId);
        updateForm.setShBy(employeeEntity.getLoginName());
        updateForm.setShTime(LocalDateTime.now());
        ShEntity shEntity = SmartBeanUtil.copy(updateForm, ShEntity.class);
        shDao.updateById(shEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        shDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer shId) {
        if (null == shId){
            return ResponseDTO.ok();
        }

        shDao.deleteById(shId);
        return ResponseDTO.ok();
    }
}
